package com.javaclimb.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.javaclimb.music.domain.Singer;
import com.javaclimb.music.service.SingerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/singer")
public class SingerController {

    @Autowired
    private SingerService singerService;

    // http://localhost:8888/SingerController/selectSinger
    @RequestMapping(value = "/selectSinger",method = RequestMethod.GET)
    public Object selectSinger(){
        //fastjson就量个json的转换工具
        JSONObject jsonObject = new JSONObject();
        //调用Service中的方法
        List<Singer> list = singerService.selectSinger();
        return list;
    }
    // http://localhost:8888/singer/insertSinger
    @RequestMapping(value = "/insertSinger",method = RequestMethod.GET)
    public int insertSinger(Singer singer) {
        //fastjson就量个json的转换工具
        JSONObject jsonObject = new JSONObject();
        int insert = singerService.insertSinger(singer);
        return insert;
    }

    @RequestMapping(value = "/updateSinger",method = RequestMethod.GET)
    public String updateSinger(Singer singer) {
        //fastjson就量个json的转换工具
        JSONObject jsonObject = new JSONObject();
        //调用Service中的方法
        int result = singerService.updateSinger(singer);
        if(result >= 1){
            return "更新成功！";
        }else{
            return "更新失败！";
        }
    }

    @RequestMapping(value = "/deleteSinger",method = RequestMethod.GET)
    public String deleteSinger(Integer id) {
        //fastjson就量个json的转换工具
        JSONObject jsonObject = new JSONObject();
        //调用Service中的方法
        int delete = singerService.deleteSinger(id);
        if(delete >= 1){
            return "删除成功！";
        }else{
            return "删除失败！";
        }
    }

    @RequestMapping(value = "/likeSinger",method = RequestMethod.GET)
    public int likeSinger(String name) {
        //fastjson就量个json的转换工具
        JSONObject jsonObject = new JSONObject();
        //调用Service中的方法
        int like = singerService.likeSinger(name);
        return like;
    }
}
